package com.example.wx.demo.wx;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangd
 */
@Slf4j
@RestController
@RequestMapping("/api/v1")
public class AccountController {

    /**
     * 定义 AccountService
     */
    private final AccountService accountService;

    /**
     * 构造方法
     *
     * @param accountService AccountService
     */
    @Autowired
    public AccountController(AccountService accountService) {
        this.accountService = accountService;
    }


    /**
     * 根据微信获取凭证
     *
     * @param openId 微信
     * @return BingingResultView
     */
    @PostMapping("/account/token/{openId}")
    public String getAccountTokenWeixin(@PathVariable("openId") String openId) {
        log.info("根据微信获取凭证,请求参数:{}", openId);
        return accountService.getAccountTokenWeixin(openId);
    }


    /**
     * 获取openid
     *
     * @param code 微信code
     * @return openid
     */
    @GetMapping("/openId/{code:.+}")
    public String getOpenid(@PathVariable("code") String code) {
        return accountService.getOpenId(code);
    }

    /**
     * 检查是否关注
     *
     * @param openid openid
     * @return boolean
     */
    @GetMapping("/follow/{openId:.+}")
    public boolean checkIsFollow(@PathVariable("openId") String openid) {
        log.info("判断用户是否关注：openid={}", openid);
        return accountService.checkIsFollow(openid);
    }

    /**
     * 获取签名
     *
     * @param url request的url
     * @return String
     */
    @GetMapping("/js/sign")
    public String getJSSign(@RequestParam String url) {
        return accountService.getJSSign(url);
    }
}
